//
//  WHMainController.swift
//  microblog
//
//  Created by Apple on 15/11/23.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit

class WHMainController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // 改变选中的颜色
        tabBar.tintColor = UIColor.orangeColor()
        //首页
        let homeVc = WHHomeController()
        //添加
        addChildViewController(homeVc,title: "首页", imageName:"tabbar_home")
        
        //消息
        let messageVc = WHMessageController()
        //添加
        addChildViewController(messageVc,title: "消息", imageName:"tabbar_message_center")
        
        // 使用一个空的来顶替位置0
        addChildViewController(UIViewController())
        
        //发现
        let DiscoverVc = WHDiscoverController()
        //添加
        addChildViewController(DiscoverVc,title: "发现", imageName:"tabbar_discover")
        
        //我
        let profileVc = WHProfileController()
        //添加
        addChildViewController(profileVc,title: "我", imageName:"tabbar_profile")
    }
    
    //设置frame
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        let width = tabBar.bounds.width / 5
        //把按钮拉大，挡住
        let frame = CGRect(x: 2 * width-5, y: 0, width: width+10, height: tabBar.bounds.height)
        composebtn.frame = frame
    }
    
    /**
    自定义添加tabbar子控制器
    
    - parameter controller: 子控制器
    - parameter title:      子控制器的标题
    - parameter imageName:  子控制器的图片名称
    */
    // private 表示只有本类或当前文件的其他类可以访问
    private func addChildViewController(controller: UIViewController,title: String, imageName: String) {
        controller.title = title
        controller.tabBarItem.image = UIImage(named: imageName)
        //添加
        addChildViewController(UINavigationController(rootViewController: controller ))
    }
 
    //懒加载
    lazy var composebtn : UIButton  = {
        let button = UIButton()
        //设置参数
        //按钮图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        //按钮背景
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        //添加
        self.tabBar.addSubview(button)
        return button
        }()
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
